package com.bonc.ioc.service.impl;

import com.alibaba.fastjson.JSON;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.CollectionDao;
import com.bonc.ioc.model.CollectionInfo;
import com.bonc.ioc.service.ICollectionService;
import com.bonc.ioc.vo.CollectionVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindException;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author ZhangXu
 * @date 2021/3/17 16:20
 */
@Service
public class CollectionServiceImpl extends BaseService implements ICollectionService {
    private static final Logger log = LoggerFactory.getLogger(CollectionServiceImpl.class);

    @Resource
    private CollectionDao collectionDao;

    @Override
    public Integer addCollection(CollectionVo param) {
        try {
            CollectionInfo isCollection = collectionDao.queryCollectionByDoubleId(param);
            if (isCollection == null) {
                return collectionDao.addCollection(param);
            }else {
                param.setId(isCollection.getId());
                return collectionDao.updateCollectionEnable(param);
            }
        }catch (Exception e ) {
            return -1;
        }
    }

    @Override
    public PageResult queryCollection(CollectionVo param) throws BindException {
        HashMap<String, Object> map = this.getPageParams();
        map.put("userId", param.getUserId());
        map.put("keyword", param.getKeyword());
        boolean flag1 = StringUtils.isNotBlank(param.getStartTime());
        boolean flag2 = StringUtils.isNotBlank(param.getEndTime());
        log.info("收藏 获取收藏列表 时间判断：" + flag1 +", " + flag2 );
        if ( flag1 && flag2 ){
            map.put("startTime", param.getStartTime());
            map.put("endTime", param.getEndTime());
        }
        log.info("收藏 获取收藏列表 查询参数={}",JSON.toJSONString(map));
        return new PageResult(map, collectionDao.selectCollectionListByPage(map));
    }

    @Override
    public Integer queryIsCollection(String userId, Long id) {
        return collectionDao.queryIsCollection(userId, id);
    }

    @Override
    public Integer deleteCollection(CollectionVo param) {
        try {
            return collectionDao.updateCollectionTombstone(param);
        }catch (Exception e ) {
            return -1;
        }
    }

}
